import {DetailModel} from "../../../../models/detail.js"
import {ActionModel} from "../../../../models/action";

const detailModel = new DetailModel()
const actionModel = new ActionModel()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFollow: false,
    isCollect: false,
    worksInfo:{},
  },

  handlerClickOnCollect: function (event) {
    let value = event.detail.isCollect
    let productionId = this.data.worksInfo.productionId
    this.setData({
      isCollect: value
    })
    return value?this._collectWorks(productionId,value):this._cancelWorks(productionId,value)
  },

  _collectWorks(worksId, value) {
    actionModel.collectProduction(worksId).then(res=>{
      this.setData({
        isCollect: value
      })
      app.showInfo('已收藏！',app.SUCCESS)
    })
  },

  _cancelWorks(worksId, value) {
    actionModel.cancelProduction(worksId).then(res=>{
      this.setData({
        isCollect: value
      })
      app.showInfo('已取消收藏！',app.SUCCESS)
    })
  },

  bindButtonTap: function (event) {
    const businessId = this.data.worksInfo.productionId
    const userId = this.data.worksInfo.userVo.userId
    wx.navigateTo({
      url: '../../form/index?businessId='+businessId+'&userId='+userId+'&from=works'
    })
  },

  //图片点击事件
  imgPreview:function(event){
    let src = event.currentTarget.dataset.src;//获取data-src
    let imgList = event.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },

  bindClickFollow:function (event) {
    let value = event.detail.isFollow
    let userId = this.data.worksInfo.userVo.userId
    return value?this._followUser(userId,value):this._cancelUser(userId,value)
  },

  bindOnChatView:function () {
    let userId = this.data.worksInfo.userVo.userId
    let nickname = this.data.worksInfo.userVo.nickname
    wx.navigateTo({
      url: '/pages/msg/private/index?userId='+userId+'&nickname='+nickname
    })
  },

  _init(productionId) {
    wx.showLoading({
      title: '正在加载中...',
    })
    detailModel.getProductionDetail(productionId).then(res=>{
      this.setData({
        worksInfo:res.data,
        isCollect: res.data.collect,
        isFollow: res.data.userVo.follow
      })
      wx.hideLoading()
    })
  },

  _followUser(userId,value) {
    actionModel.followUser(userId).then(res=>{

      this.setData({
        isFollow: value
      })
      app.showInfo('已关注！',app.SUCCESS)
    })
  },

  _cancelUser(userId,value) {
    actionModel.cancelUser(userId).then(res=>{
      this.setData({
        isFollow: value
      })
      app.showInfo('已取消关注！',app.SUCCESS)
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._init(options.id)
    let isIphoneX = app.globalData.isIphoneX;
    this.setData({
      isIphoneX: isIphoneX
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      desc: '推荐作品:'+this.data.worksInfo.name+' - PHOS',
      path: '/pages/index/details/works/index?id='+this.data.worksInfo.productionId,
      imageUrl:this.data.worksInfo.imageList[0]
    }
  }
})
